package string;

import java.util.HashMap;
import java.util.Map;

/*赎金信*/
public class QuestionTwo {
    //ransomNote 是否可以由magazine组成  magazine 中的每个字符只能在 ransomNote 中使用一次。
    public boolean canConstruct(String ransomNote, String magazine) {
        if (ransomNote.length() < magazine.length())return false;
        HashMap<Character, Integer> map1 = new HashMap<>();
        for (int i = 0; i < ransomNote.length(); i++) {
            map1.put(ransomNote.charAt(i),map1.getOrDefault(ransomNote.charAt(i),0)+1);
        }
        HashMap<Character, Integer> map2 = new HashMap<>();
        for (int i = 0; i < magazine.length(); i++) {
            map2.put(magazine.charAt(i),map2.getOrDefault(magazine.charAt(i),0)+1);
        }
        boolean flag = true;
        for (Map.Entry<Character, Integer> entry : map1.entrySet()) {
            if (!map2.containsKey(entry.getKey()) || map2.get(entry.getKey()) < entry.getValue()){
                flag = false;
            }
        }
        return flag;
    }
}
